package com.handinfo.uimenu;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.handinfo.engine.AbstractGameScene;
import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameManager;
import com.handinfo.engine.resource.ObjectAdapter;
import com.handinfo.engine.resource.ResHead;
import com.handinfo.engine.resource.ResourceManager;
import com.handinfo.engine.resource.SerializeableImage;
import com.handinfo.uimenu.window.UIMain;
import com.handinfo.uimenu.window.UIShowData;
import com.handinfo.util.Tools;

public class UIManager implements UIResourceConst, UIPalette
{
	private static UIManager s_instance;
	public static int TIME;
	/** 所有颜色 */
	private static int[] COLORS_PALETTE;
	/** 窗体层框颜色 */
	private static int[] COLOR_WIN_BORDER;
	/** 滚动条颜色 */
	private static int[] COLOR_SCROLL_BAR;
	/** 以分钟计时的条 */
	private static int[] COLOR_TIME_BAR;
	/** 内层窗体框颜色 */
	private static int[] COLOR_SUBCOAT_BORDER;
	/** UI图片 */
	public static Image IMAGE_BORDER_CORNER = null;// 边角修饰
	public static Image IMAGE_XINGJING = null;// 星号#号
	public static Image IMAGE_MAIL_UNREAD = null;// 未读邮件标示
	public static Image IMAGE_CHOOSE = null;// 勾，选中的标志
	public static Image IMAGE_LABEL_ARR = null;// 标签箭头
	public static Image IMAGE_JEWEL_RECT = null;// 宝石格子
	public static Image IMAGE_JEWEL = null;// 宝石
	public static Image IMAGE_JIAOBIAO = null;// 标签（表情处）
	public static Image IMAGE_BIAOQIAN = null;// 标签（表情处）
	public static Image IMAGE_ACTORITEM_LOCK = null;// 小锁
	public static Image IMAGE_SCROLL_RULING = null;// 下拉 柄
	public static Image IMAGE_SCROLL_ARR = null;// 下拉 箭头
	public static Image IMAGE_ITEM_CLOSE = null;// 窗格 - 暗色
	public static Image IMAGE_ITEM_OPEN = null;// 窗格 - 亮色
	public static Image IMAGE_MONEY_RMB = null;// 游戏币中人民币
	public static Image IMAGE_MONEY_COPPER = null;// 游戏币中铜币
	public static Image IMAGE_MONEY_SILVER = null;// 游戏币中银币
	public static Image IMAGE_MONEY_GOLD = null;// 游戏币中金币
	public static Image IMAGE_TITlE_MIDDLE = null;
	public static Image IMAGE_TITlE_SMALL = null;// 标题 -小
	public static Image IMAGE_ARR_LIGHT = null;// 箭头-亮色
	public static Image IMAGE_ARR_DARK = null;// 箭头-暗色
	public static Image IMAGE_SEL = null;// 选中标签
	public static Image IMAGE_JINENGBASE = null;// 技能几座
	public static Image IMAGE_ADD = null;// 技能加号
	public static Image IMAGE_FIX = null;// 修理提示
	public static Image IMAGE_LEFT_SOFT = null;// 左
	public static Image IMAGE_RIGHT_SOFT = null;// 右
	public static Image IMAGE_YINDAO = null;// 引导
	public static Image IMAGE_BAG_WORDS = null;// 背包字
	public static Image IMAGE_YINPIAO = null;//银票
	public static Image IMAGE_LABEL_SKILL = null;//技能
	public static Image IMAGE_LABEL_JUEXING = null;//觉醒
	public static Image IMAGE_LABEL_KUORONG = null;//扩容
	public static Image IMAGE_LABEL_TRIM = null;//整理
	public static Image IMAGE_LABEL_CHAT = null;//聊天标签
	public static Image IMAGE_LABEL_FUNCATION = null;//功能
	public static Image IMAGE_FIGHT = null;//战斗
	public static Image IMAGE_GOU = null;//勾
	public static Image IMAGE_CHA = null;//叉
	public static Image IMAGE_BODER_SPEAKER_MESSAGE = null;//聊天框
	public static Image IMAGE_ZHANGONG = null;//战功
	public static Image IMAGE_WUPIN = null;//快捷设置道具图标
	public static Image IMAGE_REXIAOZHONG = null;//热销中
	public static Image IMAGE_LANJIAO = null;//蓝角
	public static Image IMAGE_XIN = null;//新，热字
	public static Image IMAGE_ZHE = null;// 折 ，热字
	public static Image IMAGE_BEIBAO_QIANGHUA = null;// 背包强化
	public static Image IMAGE_GONGLUE = null;// 攻略
	// 界面标签
	public static Image IMAGE_LABEL_BAGUALU = null;// 八卦炉
	public static Image IMAGE_LABEL_CAILIAO = null;// 材料
	public static Image IMAGE_LABEL_HUODONG = null;// 活动
	public static Image IMAGE_LABEL_JICHU = null;// 基础
	public static Image IMAGE_LABEL_JUBAOPENG = null;// 举报盆
	public static Image IMAGE_LABEL_JUQING = null;// 剧情
	public static Image IMAGE_LABEL_KEJIE = null;// 可接
	public static Image IMAGE_LABEL_QITA = null;// 其他
	public static Image IMAGE_LABEL_QUANBU = null;// 全部
	public static Image IMAGE_LABEL_RONGYU = null;// 荣誉
	public static Image IMAGE_LABEL_SHENGWANG = null;// 声望
	public static Image IMAGE_LABEL_TIANGONGFANG = null;// 天工坊
	public static Image IMAGE_LABEL_XIAOHAO = null;// 消耗
	public static Image IMAGE_LABEL_YAOQIANSHU = null;// 摇钱树
	public static Image IMAGE_LABEL_ZHANDOU = null;// 战斗
	public static Image IMAGE_LABEL_ZHUANGBEI = null;// 装备
	public static Image IMAGE_LABEL_ZHUANGTAI = null;// 状态
	// 按钮
	public static Image IMAGE_BUTTON_FANHUI = null;// 返回
	public static Image IMAGE_BUTTON_QUEDING = null;// 确定
	// 菜单
	public static Image IMAGE_MENU_BEIBAO = null;// 背包
	public static Image IMAGE_MENU_DITU = null;// 地图
	public static Image IMAGE_MENU_DONGFU = null;// 洞府
	public static Image IMAGE_MENU_JINENG = null;// 技能
	public static Image IMAGE_MENU_RENWU = null;// 任务
//	public static Image IMAGE_MENU_QICHONG = null;// 骑宠
	public static Image IMAGE_MENU_SHANGCHENG = null;// 商城
	public static Image IMAGE_MENU_SHEZHI = null;// 设置
	public static Image IMAGE_MENU_SHUXING = null;// 属性
	public static Image IMAGE_MENU_TUICHUYOUXI = null;// 退出游戏
	public static Image IMAGE_MENU_ZIDONGDAGUA = null;// 自动打怪
	public static Image IMAGE_MENU_DUIWU = null;// 队伍
	public static Image IMAGE_MENU_HUODONG = null;// 活动
	public static Image IMAGE_MENU_JIAZU = null;// 家族
	public static Image IMAGE_MENU_LIAOTIAN = null;// 聊天
	public static Image IMAGE_MENU_PAIHANG = null;// 排行
	public static Image IMAGE_MENU_PAIMAI = null;// 门派
	public static Image IMAGE_MENU_QIEHUANJUESE = null;// 切换角色
	public static Image IMAGE_MENU_SHEHUI = null;// 社会
	public static Image IMAGE_MENU_YOUJIAN = null;// 邮件
	public static Image IMAGE_MENU_FUWU = null;// 服务

	//d 按钮
	public static Image IMAGE_BUTTON_CAOZUO = null;//操作
	public static Image IMAGE_BUTTON_JIESHOU = null;//接受
	public static Image IMAGE_BUTTON_WANCHENG = null;//完成
	public static Image IMAGE_BUTTON_XUNLU = null;//寻路
	public static Image IMAGE_BUTTON_AN5JIAN = null;//按5键寻路
	public static Image IMAGE_BUTTON_KAISHI = null;//开始
	
	public static Image IMAGE_SYSTEMTIME_XIAONAOZHONG = null;//小闹钟
	public static Image[] IMAGE_SMALLWORLD_AREA = null;//0战斗区  1安全区

	private UIManager()
	{

	}

	public static UIManager getInstance()
	{
		return s_instance;
	}

	/**
	 * 创建实例，省却调用时候的判断 2012-3-1 上午05:18:57 venizeng
	 */
	public static void createInstance()
	{
		s_instance = new UIManager();
	}

	/**
	 * 从RMS中获得调色板颜色 ,需在创建时调用一次
	 * @2012-2-29 下午07:54:32 venizeng
	 */
	public static void createColors()
	{
		COLORS_PALETTE = new int[COLOR_NUM];
		COLORS_PALETTE[COLOR_BACKGROUND] = 0xDD3B2415;
		COLORS_PALETTE[COLOR_TITLE] = 0x395A5C;
		COLORS_PALETTE[COLOR_CBD_DEEP] = 0x846050;
		COLORS_PALETTE[COLOR_CBD_LIGHT] = 0xB5907E;
		COLORS_PALETTE[COLOR_CBD_SELECT] = 0xB4B39B;
		COLORS_PALETTE[COLOR_WORD_NOS] = 0xF8EC89;
		// COLORS_PALETTE[COLOR_WORD_YESS] = 0x230200;
		COLORS_PALETTE[COLOR_WORD_YESS] = 0xffffff;
		COLORS_PALETTE[COLOR_SELECT_BOX_OUTER] = 0X4D0101;
		COLORS_PALETTE[COLOR_SELECT_BOX_INNER] = 0XFFF330;
		COLORS_PALETTE[COLOR_TIME_BAR_UP] = 0xFF4601;
		COLORS_PALETTE[COLOR_TIME_BAR_DOWN] = 0xF20301;
		COLORS_PALETTE[COLOR_MONEYGAME_SEL] = 0x839803;
		COLORS_PALETTE[COLOR_CHAT_XI] = 0xf3c743;
		COLORS_PALETTE[COLOR_CHAT_SI] = 0xe748f1;
		COLORS_PALETTE[COLOR_CHAT_TEAM] = 0x9196ff;
		COLORS_PALETTE[COLOR_CHAT_ZU] = 0xf3c743;
		COLORS_PALETTE[COLOR_CHAT_QU] = 0xf5e9c4;
		COLORS_PALETTE[COLOR_CHAT_WORLD] = 0xf3c743;
		COLORS_PALETTE[COLOR_CHAT_FAMILY] = 0x67d267;
		COLORS_PALETTE[COLOR_RECT_SEL] = 0x5b7582;
		//
		COLOR_WIN_BORDER = new int[] { 0x580D00, 0xFFFFE1, 0x484232, 0x230200 };
		COLOR_SUBCOAT_BORDER = new int[] { 0x230200, 0xD5C181, 0x685A4B };
		COLOR_SCROLL_BAR = new int[] { 0x520602, 0xFFD449, 0x966301, 0x520602 };
		COLOR_TIME_BAR = new int[] { 0x491414, 0xFFFFAB, 0x714500, 0x000000, 0x000000, 0x860808, 0xFFD749, 0x4D0101 };
	}

	public static void createImages()
	{
		IMAGE_SEL = s_instance.coinImage(RES_SELECT_CORNER);// 选中
		IMAGE_ARR_DARK = s_instance.coinImage(RES_ARR_DARK);//
		IMAGE_ARR_LIGHT = s_instance.coinImage(RES_ARR_LIGHT);
		IMAGE_TITlE_SMALL = s_instance.coinImage(RES_TITLE_SMALL);
		IMAGE_TITlE_MIDDLE = s_instance.coinImage(RES_TITLE_MIDDLE);
		IMAGE_MONEY_GOLD = s_instance.coinImage(RES_MONEY_GOLD);
		IMAGE_MONEY_SILVER = s_instance.coinImage(RES_MONEY_SILVER);
		IMAGE_MONEY_COPPER = s_instance.coinImage(RES_MONEY_COPPER);
		IMAGE_MONEY_RMB = s_instance.coinImage(RES_MONEY_RMB);
		IMAGE_ITEM_OPEN = s_instance.coinImage(RES_ITEM_OPEN);
		IMAGE_ITEM_CLOSE = getInstance().coinImage(RES_ITEM_CLOSE);
		IMAGE_SCROLL_ARR = s_instance.coinImage(RES_SCROLL_BAR_ARROWS);
		IMAGE_SCROLL_RULING = getInstance().coinImage(RES_SCROLL_BAR_RULING);
		IMAGE_ACTORITEM_LOCK = s_instance.coinImage(RES_ACTORITEM_LOCK);
		IMAGE_BIAOQIAN = s_instance.coinImage(UIResourceConst.RES_BIAOQING);
		IMAGE_CHOOSE = s_instance.coinImage(UIResourceConst.RES_CHOOSED);
		IMAGE_JIAOBIAO = s_instance.coinImage(UIResourceConst.RES_TIME_BAR_LEFT);
		IMAGE_MAIL_UNREAD = s_instance.coinImage(UIResourceConst.RES_MAIL);
		IMAGE_XINGJING = s_instance.coinImage(UIResourceConst.RES_XINGJING);
		IMAGE_JEWEL = s_instance.coinImage(UIResourceConst.RES_JEWEL);// 宝石
		IMAGE_JEWEL_RECT = s_instance.coinImage(UIResourceConst.RES_JEWEL_RECT);// 宝石格子
		IMAGE_LABEL_ARR = s_instance.coinImage(UIResourceConst.RES_LABEL_ARR);// 标签箭头
		IMAGE_BORDER_CORNER = s_instance.coinImage(UIResourceConst.RES_BORDER_CORNER);
		IMAGE_JINENGBASE = s_instance.coinImage(UIResourceConst.RES_JINENGBASE);
		IMAGE_ADD = s_instance.coinImage(UIResourceConst.RES_ADD);
		IMAGE_FIX = s_instance.coinImage("xiulitubiao.png");
		IMAGE_LEFT_SOFT = s_instance.coinImage("zuo_soft.png");
		IMAGE_RIGHT_SOFT = s_instance.coinImage("you_soft.png");//右软键
		IMAGE_YINDAO = s_instance.coinImage("yindao_arr.png");//引导图标
		IMAGE_BAG_WORDS = s_instance.coinImage("zhuangbeidi.png");
		IMAGE_YINPIAO = s_instance.coinImage("chaopiao.png");
		IMAGE_LABEL_SKILL = s_instance.coinImage("biaoqian_jineng.png");
		IMAGE_LABEL_JUEXING = s_instance.coinImage("biaoqian_juexing.png");
		IMAGE_LABEL_KUORONG = s_instance.coinImage("biaoqian_kuorong.png");
		IMAGE_LABEL_TRIM = s_instance.coinImage("biaoqian_zhengli.png");
		IMAGE_LABEL_CHAT = s_instance.coinImage("biaoqian_liaotian.png");
		IMAGE_LABEL_FUNCATION = s_instance.coinImage("biaoqian_gongneng.png");
		IMAGE_FIGHT = s_instance.coinImage("fight.png");
		IMAGE_GOU = s_instance.coinImage("gou.png");
		IMAGE_CHA = s_instance.coinImage("cha.png");
		IMAGE_BODER_SPEAKER_MESSAGE = s_instance.coinImage("boder_speaker_message.png");
		IMAGE_ZHANGONG = s_instance.coinImage("zhangong.png");
		IMAGE_WUPIN = s_instance.coinImage("biaoqian_wupin.png");
		IMAGE_REXIAOZHONG = s_instance.coinImage("rexiaozhong.png");
		IMAGE_LANJIAO = s_instance.coinImage("lanjiao.png");
		IMAGE_XIN = s_instance.coinImage("shangcheng_xin.png");
		IMAGE_ZHE = s_instance.coinImage("shangcheng_zhe.png");
		IMAGE_BEIBAO_QIANGHUA = s_instance.coinImage("biaoqian_qianghua.png");
		IMAGE_GONGLUE = s_instance.coinImage("you_gonglue.png");
		// 界面标签
		IMAGE_LABEL_BAGUALU = s_instance.coinImage("biaoqian_bagualu.png");// 八卦炉
		IMAGE_LABEL_CAILIAO = s_instance.coinImage("biaoqian_cailiao.png");// 材料
		IMAGE_LABEL_HUODONG = s_instance.coinImage("biaoqian_huodong.png");// 活动
		IMAGE_LABEL_JICHU = s_instance.coinImage("biaoqian_jichu.png");// 基础
		IMAGE_LABEL_JUBAOPENG = s_instance.coinImage("biaoqian_jubaopen.png");// 聚宝盆
		IMAGE_LABEL_JUQING = s_instance.coinImage("biaoqian_juqing.png");// 剧情
		IMAGE_LABEL_KEJIE = s_instance.coinImage("biaoqian_kejie.png");// 可接
		IMAGE_LABEL_QITA = s_instance.coinImage("biaoqian_qita.png");// 其他
		IMAGE_LABEL_QUANBU = s_instance.coinImage("biaoqian_quanbu.png");// 全部
		IMAGE_LABEL_RONGYU = s_instance.coinImage("biaoqian_rongyu.png");// 荣誉
		IMAGE_LABEL_SHENGWANG = s_instance.coinImage("biaoqian_shengwang.png");// 声望
		IMAGE_LABEL_TIANGONGFANG = s_instance.coinImage("biaoqian_tiangongfang.png");// 天工坊
		IMAGE_LABEL_XIAOHAO = s_instance.coinImage("biaoqian_xiaohao.png");// 消耗
		IMAGE_LABEL_YAOQIANSHU = s_instance.coinImage("biaoqian_yaoqianshu.png");// 摇钱树
		IMAGE_LABEL_ZHANDOU = s_instance.coinImage("biaoqian_zhandou.png");// 战斗
		IMAGE_LABEL_ZHUANGBEI = s_instance.coinImage("biaoqian_zhuangbei.png");// 装备
		IMAGE_LABEL_ZHUANGTAI = s_instance.coinImage("biaoqian_zhuangtai.png");// 状态
		// 按钮
		IMAGE_BUTTON_FANHUI = s_instance.coinImage("button_fanhui.png");// 返回
		IMAGE_BUTTON_QUEDING = s_instance.coinImage("button_queding.png");// 确定
		// 菜单
		IMAGE_MENU_BEIBAO = s_instance.coinImage("you_beibao.png");// 背包
		IMAGE_MENU_DITU = s_instance.coinImage("you_ditu.png");// 地图
		IMAGE_MENU_DONGFU = s_instance.coinImage("you_dongfu.png");// 洞府
		IMAGE_MENU_JINENG = s_instance.coinImage("you_jineng.png");// 技能
		IMAGE_MENU_RENWU = s_instance.coinImage("zuo_renwu.png");// 任务
//		IMAGE_MENU_QICHONG = s_instance.createImage("you_qichong.png");// 骑宠
		IMAGE_MENU_SHANGCHENG = s_instance.coinImage("you_shangcheng.png");// 商城
		IMAGE_MENU_SHEZHI = s_instance.coinImage("you_shezhi.png");// 设置
		IMAGE_MENU_SHUXING = s_instance.coinImage("you_shuxing.png");// 属性
		IMAGE_MENU_TUICHUYOUXI = s_instance.coinImage("you_tuichuyouxi.png");// 退出游戏
		IMAGE_MENU_ZIDONGDAGUA = s_instance.coinImage("you_zidongdaguai.png");// 自动打怪
		IMAGE_MENU_DUIWU = s_instance.coinImage("zuo_duiwu.png");// 队伍
		IMAGE_MENU_HUODONG = s_instance.coinImage("zuo_huodong.png");// 活动
		IMAGE_MENU_JIAZU = s_instance.coinImage("zuo_jiazu.png");// 家族
		IMAGE_MENU_LIAOTIAN = s_instance.coinImage("zuo_liaotian.png");// 聊天
		IMAGE_MENU_PAIHANG = s_instance.coinImage("zuo_paihang.png");// 排行
		IMAGE_MENU_PAIMAI = s_instance.coinImage("zuo_paimai.png");// 排行
		IMAGE_MENU_QIEHUANJUESE = s_instance.coinImage("zuo_qiehuanjuese.png");// 切换角色
		IMAGE_MENU_SHEHUI = s_instance.coinImage("zuo_shehui.png");// 社会
		IMAGE_MENU_YOUJIAN = s_instance.coinImage("zuo_youjian.png");// 邮件
		IMAGE_MENU_FUWU = s_instance.coinImage("zuo_fuwu.png");// 预留
		//d 按钮
		IMAGE_BUTTON_CAOZUO = s_instance.coinImage("w_caozuo.png");
		IMAGE_BUTTON_JIESHOU = s_instance.coinImage("w_jieshou.png");
		IMAGE_BUTTON_WANCHENG = s_instance.coinImage("w_wancheng.png");
		IMAGE_BUTTON_XUNLU = s_instance.coinImage("w_xunlu.png");
		IMAGE_BUTTON_AN5JIAN = s_instance.coinImage("w_an5jian.png");
		IMAGE_BUTTON_KAISHI = s_instance.coinImage("w_kaishi.png");
		IMAGE_SYSTEMTIME_XIAONAOZHONG = s_instance.coinImage("xiaonaozhong.png");
		IMAGE_SMALLWORLD_AREA = new Image[2];
		IMAGE_SMALLWORLD_AREA[0] = s_instance.coinImage("fararea.png");
		IMAGE_SMALLWORLD_AREA[1] = s_instance.coinImage("freearea.png");
		initResource();
	}

	public static void initResource()
	{
		UIManager.getInstance().coinImage(UIManager.RES_MAIN_HEAD_SELF);
		UIManager.getInstance().coinImage(UIManager.RES_MAIN_HEAD_OTHER);
		UIManager.getInstance().coinImage(UIManager.RES_SHORTCUT_RECT);
		UIManager.getInstance().coinImage(UIManager.RES_SHORTCUT_RECT2);
		UIManager.getInstance().coinImage(UIManager.RES_SHORTCUT_DECORATE);
		UIManager.getInstance().coinImage(UIManager.RES_SHORTCUT_ARROW1);
		UIManager.getInstance().coinImage(UIManager.RES_SHORTCUT_ARROW2);
		UIManager.getInstance().coinImage(UIManager.RES_MAIN_LEADER);
		UIManager.getInstance().coinImage(UIManager.RES_FUZHOU);
		UIManager.getInstance().coinImage(UIManager.RES_FUZHOUHUOYAN);
	}

	public static int[] getColorsScrollBar()
	{
		return COLOR_SCROLL_BAR;
	}

	public static int[] getColorsSubcoatBorder()
	{
		return COLOR_SUBCOAT_BORDER;
	}

	public static int[] getColorsWinBorder()
	{
		return COLOR_WIN_BORDER;
	}

	public static int getPaletteColor(int index)
	{
		return COLORS_PALETTE[index];
	}

	public static int[] getTimeBarColor()
	{
		return COLOR_TIME_BAR;
	}

	/**
	 * 窗体集合
	 */
	public Vector m_wins = new Vector();

	/** 半透的窗体背景色 */
	public static final int COLOR_BACKGROUND = 0;

	/**
	 * 获得指定索引的窗体
	 * 
	 * @param inx
	 * @return
	 */
	public UIAbstractWindow getWindowAt(int inx)
	{
		return (UIAbstractWindow) m_wins.elementAt(inx);
	}

	/**
	 * 获得指定Id的窗体
	 * 
	 * @param id
	 * @return
	 */
	public UIAbstractWindow getWindowById(int id)
	{
		if (m_wins.size() <= 0) {
			return null;
		}
		UIAbstractWindow win;
		for (int i = 0; i < m_wins.size(); i++) {
			win = (UIAbstractWindow) m_wins.elementAt(i);
			if (win.m_id == id)
				return win;
		}
		return null;
	}

	/**
	 * 添加窗体到集合中
	 * 
	 * @param win
	 */
	public void addWindow(UIAbstractWindow win)
	{
		m_wins.addElement(win);
		win.showNotify();
		updateScenePaintFlag();
	}

	/**
	 * 插入窗体到集合中的inx位置
	 * 
	 * @param win
	 * @param inx
	 */
	public void insertWindowAt(UIAbstractWindow win, int inx)
	{
		m_wins.insertElementAt(win, inx);
	}

	/**
	 * 从集合中移除窗体
	 * 
	 * @param win
	 */
	public boolean removeWindow(UIAbstractWindow win)
	{
		if (win == null) {// zeng 防止当前窗体为空，出现空指针情况
			return true;
		}
		boolean temp = m_wins.removeElement(win);
		resetWindow();
		updateScenePaintFlag();
		win.hideNotify();
		return temp;
	}

	/**
	 * 从集合中移除指定索引的窗体
	 * 
	 * @param inx
	 */
	public void removeWindowAt(int inx)
	{
		m_wins.removeElementAt(inx);
		resetWindow();
		updateScenePaintFlag();
	}

	/**
	 * 从集合中移除指定Id的窗体
	 * 
	 * @param id
	 * @return
	 */
	public boolean removeWindowById(int id)
	{
		if (m_wins.size() <= 0)
			return false;
		UIAbstractWindow win;
		for (int i = 0; i < m_wins.size(); i++) {
			win = (UIAbstractWindow) m_wins.elementAt(i);
			if (win.m_id == id) {
				m_wins.removeElementAt(i);
				updateScenePaintFlag();
				return true;
			}
		}
		return false;
	}

	public void removeAllWindows()
	{
		Tools.debugPrintln("移除所有的UI窗体");
		int size = m_wins.size();
		for (int i = size - 1; i >= 0; i--) {
			getInstance().removeWindow((UIAbstractWindow) m_wins.elementAt(i));
		}
	}

	/**
	 * 移除除主窗口外所有窗体
	 */
	public void removeOtherWindows()
	{
		Tools.debugPrintln("移除除主窗口外所有窗体");
		int size = m_wins.size();
		for (int i = size - 1; i > 0; i--) {
			getInstance().removeWindow((UIAbstractWindow) m_wins.elementAt(i));
		}
	}

	private void resetWindow()
	{
		// if (m_wins.size() == 0) {
		// UiManager.s_isControlledByOld = true;
		// }
	}

	/**
	 * 根据Id创建窗体
	 * 
	 * @param id
	 * @return
	 */
	public UIAbstractWindow createWindow(int id)
	{
		UIAbstractWindow window = UIWindowFactory.createWindow(id);
		addWindow(window);		
		return window;
	}

	/**
	 * 获得当前被焦点的窗体
	 * 
	 * @return
	 */
	public UIAbstractWindow getCurFocusedWindow()
	{
		UIAbstractWindow win = null;
		if (m_wins.size() <= 0) {
			return null;
		}
		for (int i = m_wins.size() - 1; i >= 0; i--) {
			UIAbstractWindow win1 = (UIAbstractWindow) m_wins.elementAt(i);
			if (win1.m_focusable && win == null) {
				win = win1;
//				win1.setTouchFocus(true);
			} else {
//				win1.setTouchFocus(false);
			}
		}
		return win;
	}
	
//	public int checkCurFocusedWindow()
//	{
//		UIAbstractWindow win = null;
////		if (m_wins.size() <= 0) {
////			return null;
////		}
//		for (int i = m_wins.size() - 1; i >= 0; i--) {
//			UIAbstractWindow win1 = (UIAbstractWindow) m_wins.elementAt(i);
//			if (win1.m_focusable && win == null) {
//				win = win1;
//				win1.setTouchFocus(true);
//			} else {
//				win1.setTouchFocus(false);
//			}
//		}
//		return m_wins.size();
//	}

	
	/**
	 * 创建UI图片
	 * @param path
	 * @return
	 */
	public Image coinImage(String path)//createImage
	{
		return ((SerializeableImage) ResourceManager.getInstance().loadResource(
						GameManager.getInstance().m_resource_static,
						new ResHead(ObjectAdapter.RESOURCE_TYPE_PNG, RES_UI_PATH + path, 1))).m_image;
	}

	/**
	 * 绘制
	 * 
	 * @param g
	 */
	public void paint(Graphics g)
	{
		UIAbstractWindow win;
		for (int i = 0; i < m_wins.size(); i++) {
			win = (UIAbstractWindow) m_wins.elementAt(i);
			if (win.m_isPaint) {
				win.paint(g);
			}
		}
		GameManager gm = GameManager.getInstance();
		try {
			if (gm.m_isInTeach) {
				if (gm.m_types_teach[gm.m_index_teach] == 0) {
					int w = UITools.W_BG >> 1;
					int h = gm.m_desc_teach[gm.m_index_teach].length * AbstractGameScreen.FONT_H + (UITools.IH_RECT_CONTENT<<1);
					int tempX = (UITools.W_BG - w) >> 1;
					int tempY = (UITools.H_BG - h) >> 1;
					UITools.paintYindaoDescribe(g, tempX, tempY, w, h, gm.m_desc_teach[gm.m_index_teach]);
				}
				else {
					UITools.paintYindao(g, gm.m_desc_teach[gm.m_index_teach], gm.m_infos_teach[gm.m_index_teach]);
				}
			}
		}
		catch (Exception e) {
			gm.getOutOfTeach();
		}
		
		UIShowData data = GameManager.getInstance().m_showData;
		if( data !=null ){
			data.paint(g);
		}
	}

	/**
	 * 逻辑
	 */
	public void logic()
	{
		TIME++;
		GameManager gm = GameManager.getInstance();
		label1 :
		try {
			if (gm.m_isInTeach) {
				int[] keys = gm.m_keys_teach[gm.m_index_teach];
				if (keys != null) {
					for (int i = 0, len = keys.length; i < len; i++) {
						if (AbstractGameScreen.haveKeyPressed(keys[i])) {
							gm.m_index_teach++;
							if (gm.m_index_teach >= gm.m_desc_teach.length) {
								gm.getOutOfTeach();
								break;
							}
							break label1;
						}
					}
				}
				int[] keys_skipes = gm.m_keys_skip_teach[gm.m_index_teach];
				if (keys_skipes != null) {
					for (int i = 0, len = keys_skipes.length; i < len; i++) {
						if (AbstractGameScreen.haveKeyPressed(keys_skipes[i])) {
							gm.m_index_teach++;
							if (gm.m_index_teach >= gm.m_desc_teach.length) {
								gm.getOutOfTeach();
								break;
							}
						}
					}
				}
				return;
			}
		}
		catch (Exception e) {
			gm.getOutOfTeach();
		}
		UIAbstractWindow win = getCurFocusedWindow();
		if (win != null) {
			win.logic();
		}
		UIShowData data = GameManager.getInstance().m_showData;
		if( data !=null ){
			data.logicShowData();
		}
	}

	/**
	 * 更新场景绘制标识
	 */
	public void updateScenePaintFlag()
	{
		AbstractGameScene scene = GameManager.getInstance().m_scene;
		if (scene == null)
			return;
		UIAbstractWindow win = null;
		//无需判断主界面 m_wins(0)
		for (int i = m_wins.size() - 1; i >= 1; i--) {
			win = (UIAbstractWindow) m_wins.elementAt(i);
			if (win != null && !Tools.isInArray(win.m_id, UIWindowFactory.WINDOW_SMALL_IDS)) {
				scene.m_isPainting = false;
				return;
			}
		}
		scene.m_isPainting = true;
		scene.m_forceCamera = true;
//		scene.m_bg.m_updateBg = true;
	}

}
